<?php declare(strict_types=1);
/**
 * @author      xianganyall <xianganyall@gmail.com>
 * @copyright   2023-2025 owner
 **/

namespace Srv\Libs\ConfModel;

use Srv\Libs\Frame\Conf;

final class DbConfGroup
{
    private array $DbConfGroupNameList                      = [];       // DbConf List groupName

    /**
     * @return array
     * getGroupNameList
     */
    public function getGroupNameList():array
    {
        $DbConfList                 = [];
        $groupNameList              = Conf::getGroupNameList(DbConf::PREFIX);
        if(count($groupNameList) < 1) return $DbConfList;
        foreach ($groupNameList as $groupName){
            $DbConf              = $this->getByGroupName($groupName);
            if($DbConf instanceof DbConf) $DbConfList[$groupName] = $DbConf;
        }
        return $DbConfList;
    }

    /**
     * @param string $groupName
     * @return DbConf|null
     * getByGroupName
     */
    public function getByGroupName(string $groupName):?DbConf
    {
        if(strlen($groupName) < 1) return null;
        if(isset($this->DbConfGroupNameList[$groupName]) && $this->DbConfGroupNameList[$groupName] instanceof DbConf) return $this->DbConfGroupNameList[$groupName];
        $confData                                   = Conf::getGroupList(DbConf::PREFIX.$groupName.'.', false, true);
        if(count($confData) < 1) return null;
        $this->DbConfGroupNameList[$groupName]      = new DbConf($confData, $groupName);
        return $this->DbConfGroupNameList[$groupName];
    }
}